package cn.edu.gzccc.moviecms.controller;

import cn.edu.gzccc.moviecms.domain.Actor;
import cn.edu.gzccc.moviecms.domain.Movie;
import cn.edu.gzccc.moviecms.domain.Role;
import cn.edu.gzccc.moviecms.repository.ActorRepository;
import cn.edu.gzccc.moviecms.repository.MovieRepository;
import cn.edu.gzccc.moviecms.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author 杨旭锐
 * @Time 2018/4/21
 */
@Controller
@RequestMapping(value = "role")
public class RoleController {
    @Autowired
    private MovieRepository movieRepository;
    @Autowired
    private ActorRepository actorRepository;
    @Autowired
    private RoleRepository roleRepository;

    //跳转到添加演员页面
    @RequestMapping(value = "/add/{movie_id}", method = RequestMethod.GET)
    public String toAdd(Model model, @PathVariable Long movie_id) {
        Movie movie = movieRepository.findOne(movie_id);
        model.addAttribute("info", movie);
        return "role/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String doAdd(@RequestParam String actor_name,
                        @RequestParam String role_name,
                        @RequestParam Long movie_id) {
        Movie movie = movieRepository.findOne(movie_id);
        Actor actor = new Actor();
        actor.setName(actor_name);
        movie.addRole(actor, role_name);
        movieRepository.save(movie);

        return "redirect:/movie/detail/" + movie_id;
    }

    //跳转到详细页面
    @RequestMapping(value = "/detail/{actor_id}",method = RequestMethod.GET)
    public String detail(Model model,@PathVariable Long actor_id){
        Actor actor = actorRepository.findOne(actor_id);
        model.addAttribute("info",actor);
        return "role/detail";
    }


    //跳转编辑页面
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public String edit(Model model,@PathVariable Long id){
        Role role = roleRepository.findOne(id);
        model.addAttribute("info",role);
        return "role/edit";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(@RequestParam String actor_name,
                       @RequestParam String role_name,
                       @RequestParam Long id){
        Role role = roleRepository.findOne(id);
        Actor actor = new Actor();

        role.setName(role_name);
        actor.setName(actor_name);
        role.setActor(actor);

        roleRepository.save(role);
        return "redirect:/movie/detail/"+role.getMovie().getId();
    }

    //删除演员
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public  String delete(@PathVariable Long id){
        Role role = roleRepository.findOne(id);
        Long str = role.getMovie().getId();
        roleRepository.delete(id);
        return "redirect:/movie/detail/"+str;
    }
}
